package com.example.pingze.controller;

import com.example.pingze.entity.SelectedPicture;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.SelectedPictureService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/selectedPicture")
@CrossOrigin(origins = "*")
public class SelectedPictureController {
    @Autowired
    private SelectedPictureService service;
    @PostMapping("/getPicture")
    public Result<List<String>> getPicture(
            @RequestParam(name = "pageCode", required = false, defaultValue = "1") String pageCode,
            @RequestParam(name = "pageSize", required = false, defaultValue = "8") String pageSize
    ){
        int i = Integer.parseInt(pageCode);
        int j = Integer.parseInt(pageSize);
        if(i < 1){
            throw new PingZeException("页码不能小于1");
        }
        Pageable pageable = PageRequest.of(i - 1, j);
        Page page = service.getSelectedPictureList(pageable);
        List content = page.getContent();
        List<SelectedPicture> list = (List<SelectedPicture>) content;
        List<String> urlList = new ArrayList<>();
        for(SelectedPicture picture : list){
            String pictureUrl = picture.getPictureUrl();
            urlList.add(pictureUrl);
        }
        return ResultVOUtils.result(urlList);
    }

    /**
     * 后台管理系统
     * @param picture
     * @param pageCode
     * @param pageSize
     * @return
     */
    @CrossOrigin
    @PostMapping("/page")
    public Result<Page<SelectedPicture>> getList(
            SelectedPicture picture,
            @RequestParam(name="pageCode",required = false,defaultValue = "1") Integer pageCode,
            @RequestParam(name="pageSize",required = false, defaultValue = "2") Integer pageSize
    ){
        if(pageCode < 1){
            throw new PingZeException("页码不能小于1");
        }
        Sort.Order idOrder = Sort.Order.asc("id");
        Sort sort=Sort.by(idOrder);
        Pageable pageable = PageRequest.of(pageCode - 1, pageSize, sort);
        Page page = service.getList(picture,pageable);
        return ResultVOUtils.result(page);
    }

    @CrossOrigin
    @PostMapping("/add")
    public Result<SelectedPicture> add(String pictureUrl){
        SelectedPicture add = service.add(pictureUrl);
        return ResultVOUtils.result(add);
    }

    @CrossOrigin
    @PostMapping("/edit")
    public Result<SelectedPicture> edit(String id,String pictureUrl){
        SelectedPicture edit = service.edit(id,pictureUrl);
        return ResultVOUtils.result(edit);
    }

    @CrossOrigin
    @Transactional
    @PostMapping("/delete")
    public Result<String> delete(String id){
        if(id == null){
            throw new PingZeException("id为空");
        }
        SelectedPicture find = service.findSelectedPictureById(id);
        if(find != null){
            service.delete(find);
        }
        return ResultVOUtils.success("delete successful");
    }
}
